﻿using UnityEngine;
using UnityEngine.UI;

public class Wall : BaseMono, IOnExplode
{
	public bool CanDestroy = false;

	public bool hasProp = false;
	public Prop Prop;

	private Transform PropRoot;

	public override void onAwake()
	{
		base.onAwake();
		PropRoot = GameObject.FindWithTag("Root").transform.FindChild("Props");
	}

	public void OnExplode()
	{
		if (CanDestroy)
		{
			MainGame.AStarGridMgr.SetNodeCanMove(transform.position, true);
			if (hasProp)
			{
				Prop.Enable = true;
			}
			gameObject.SetActive(false);
		}
	}

	public void SetProp(PropInfo info)
	{
		hasProp = true;
//		gameObject.GetComponent<Image>().color = new Color(1,1,1,0.5f);
		GameObject go = GameRes.Ins.InstantiateImageItem();
		go.transform.SetParent(PropRoot);
		go.transform.position = transform.position;
		go.GetComponent<BoxCollider2D>().size = Vector2.one*0.5f;
		go.layer = LayerMask.NameToLayer("Prop");

		Prop = go.TryGetComponent<Prop>();
		Prop.Init(info);
		Image img = go.TryGetComponent<Image>();
		img.sprite = SignSprite(info.Type);

		go.TryGetComponent<BoxCollider2D>().isTrigger = true;
	}

	public Sprite SignSprite(PropTypy typy) {
		Sprite sprite = null;
		switch (typy) {
			case PropTypy.BombPlus:
				sprite = GameRes.Ins.LoadSprite("Items_0", "Prop/Textures/Items");
				break;
			case PropTypy.FireDisPlus:
				sprite = GameRes.Ins.LoadSprite("Items_1", "Prop/Textures/Items");
				break;
			case PropTypy.SpeedPlus:
				sprite = GameRes.Ins.LoadSprite("Items_2", "Prop/Textures/Items");
				break;
			case PropTypy.Door:
				sprite = GameRes.Ins.LoadSprite("Items_4", "Prop/Textures/Items");
				break;
		}

		return sprite;
	}

	public void Clear()
	{
		if (hasProp)
		{
			Prop.Clear();
		}
		Destroy(gameObject);
	}
}
